<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>我</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
</head>
<style>
.my-header {
    background: url('../image/user_center_bg.png') no-repeat;
    background-size: cover;
    min-height: 220px;
    position: relative;
}
.my-info {
    position: relative;
    padding: 20px 0;
    width: 100%;
    bottom: 0;
    text-align: center;
    vertical-align: center;
}
.my-info img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
}
.my-info p.nickname {
    margin-top: 5px;
    color: #ffffff;
    font-size: 18px;
}
.my-menu {
    position: absolute;
    width: 100%;
    height: 45px;
    line-height: 14px;
    left: 0;
    bottom: 0;
    text-align: center;
    background: #ffffff;
    filter:Alpha(opacity=50);
    background:rgba(255, 255, 255, 0.2) none repeat scroll 0 0 !important;
    color: #ffffff;
    font-size: 12px;
    padding-top: 10px;
}
.my-menu .aui-col-xs-3 {
    border-right: 1px solid #9bd6d5;
}
.my-menu .aui-col-xs-3:last-child {
    border-right: none;
}
.aui-list-view-cell {
    line-height: 26px;
}
</style>
<body>
    <div class="aui-content my-header">
        <div class="my-info">
            <img src="../image/widget_dface.png" id="portrait" onclick="openLogin()">
            <p class="nickname" id="name">点击头像登录</p>
        </div>
        <div class="my-menu">
            <div class="aui-col-xs-3" id="score">积分<br />0</div>
            <div class="aui-col-xs-3" id="favoritecount">收藏<br />0</div>
            <div class="aui-col-xs-3" id="followers">关注<br />0</div>
            <div class="aui-col-xs-3" id="fans">粉丝<br />0</div>
        </div>
    </div>
    <div class="aui-content aui-card">
        <ul class="aui-list-view">
            <li class="aui-list-view-cell">
                <a class="aui-arrow-right">个人资料
                </a>
            </li>
            <li class="aui-list-view-cell" tapmode onclick="openNotice()">
                <a class="aui-arrow-right">通知消息
                	<span class="aui-badge aui-badge-danger" id="notice_count" style="display: none;">0</span>
                </a>
                
            </li>
            <li class="aui-list-view-cell">
                <a class="aui-arrow-right">我的博客</a>
            </li>
            <!--li class="aui-list-view-cell" tapmode onclick="openChatRoom()">
                <a class="aui-arrow-right">聊天室(实验功能)</a>
            </li-->
        </ul>
    </div>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/common.js" ></script>
<script type="text/javascript" src="../script/OSChinaApi.js" ></script>
<script type="text/javascript">
    apiready = function () {
    	showMyInfo();
    	
		// 下拉刷新
		api.setRefreshHeaderInfo({
	        visible: true,
	        loadingImg: 'widget://image/ptr_pull.png',
	        bgColor: '#efeff4',
	        textColor: '#959595',
	        textDown: '下拉可刷新',
	        textUp: '松开立即刷新',
	        showTime: true
		}, function(ret, err){
		    //从服务器加载数据，完成后调用api.refreshHeaderLoadDone()方法恢复组件到默认状态
		    getMyInfo();
		});
		
		api.refreshHeaderLoading();

    }
    
	function showMyInfo(){
    	if( isLogin() == true ){
			$api.attr($api.byId('portrait'), 'onclick', 'logout()');
    		$api.text($api.byId('name'), $api.getStorage('user_name') );
    		$api.attr($api.byId('portrait'), 'src', $api.getStorage('user_portrait') );
    		$api.html($api.byId('score'), '积分<br />'+$api.getStorage('user_score') );
    		$api.html($api.byId('favoritecount'), '收藏<br />'+$api.getStorage('user_favoritecount') );
    		$api.html($api.byId('followers'), '关注<br />'+$api.getStorage('user_followers') );
    		$api.html($api.byId('fans'), '粉丝<br />'+$api.getStorage('user_fans') );
    		var notice_count = $api.getStorage('notice_count')
    		$api.text($api.byId('notice_count'), notice_count );
    		if( notice_count>0 ){
    			$('#notice_count').show();
    		}else{
    			$('#notice_count').hide();
    		}
    	}
	}
    
    function getMyInfo(){

    	if( isLogin() == false ){
			api.refreshHeaderLoadDone();
			$api.attr($api.byId('portrait'), 'onclick', 'openLogin()');
    		$api.text($api.byId('name'), '点击头像登录');
    		$api.attr($api.byId('portrait'), 'src', '../image/widget_dface.png');
    		$api.html($api.byId('score'), '积分<br />0');
    		$api.html($api.byId('favoritecount'), '收藏<br />0');
    		$api.html($api.byId('followers'), '关注<br />0');
    		$api.html($api.byId('fans'), '粉丝<br />0');
    		$api.text($api.byId('notice_count'), 0);
    		$('#notice_count').hide();
			
			return false;
    	}
    
    	var url = OpenAPI.my_information + '?uid=' + $api.getStorage('user_uid');
		api.ajax({
		    url: url,
		    method: 'get',
		    timeout: 30,
		    dataType: 'text',
		    returnAll:false,
		},function(ret,err){
			api.refreshHeaderLoadDone();
		    if (ret) {
				var trans = api.require('trans');
				trans.parse({
				    data:ret
				},function(ret,err){
				    if( typeof(ret.oschina.user) == "object") {

				    	$api.attr($api.byId('portrait'), 'onclick', 'logout()');
				    
						$api.setStorage('user_name', ret.oschina.user.name);
			    		$api.text($api.byId('name'), ret.oschina.user.name);
			    		var portrait = ret.oschina.user.portrait
			    		if( ret.oschina.user.portrait ){
			    			portrait = ret.oschina.user.portrait;
			    		}else{
			    			portrait = '../image/widget_dface.png';
			    		}
			    		$api.setStorage('user_portrait', portrait);
			    		$api.attr($api.byId('portrait'), 'src', portrait);
			    		
			    		$api.setStorage('user_score', ret.oschina.user.score);
			    		$api.html($api.byId('score'), '积分<br />'+ret.oschina.user.score);
			    		
			    		$api.setStorage('user_favoritecount', ret.oschina.user.favoritecount);
			    		$api.html($api.byId('favoritecount'), '收藏<br />'+ret.oschina.user.favoritecount);
			    		
			    		$api.setStorage('user_followers', ret.oschina.user.followers);
			    		$api.html($api.byId('followers'), '关注<br />'+ret.oschina.user.followers);
			    		
			    		$api.setStorage('user_fans', ret.oschina.user.fans);
			    		$api.html($api.byId('fans'), '粉丝<br />'+ret.oschina.user.fans);
			    		
			    		$api.setStorage('user_jointime', ret.oschina.user.jointime);
			    		$api.setStorage('user_gender', ret.oschina.user.gender);
			    		$api.setStorage('user_location', ret.oschina.user.from);
			    		$api.setStorage('user_devplatform', ret.oschina.user.devplatform);
			    		$api.setStorage('user_expertise', ret.oschina.user.expertise);

			    		$api.setStorage('notice_atmeCount', ret.oschina.notice.atmeCount);		// 新@我个数
			    		$api.setStorage('notice_msgCount', ret.oschina.notice.msgCount);		// 新私信个数
			    		$api.setStorage('notice_reviewCount', ret.oschina.notice.reviewCount);	// 新评论个数
			    		$api.setStorage('notice_newFansCount', ret.oschina.notice.newFansCount);// 新评论个数
			    		$api.setStorage('notice_newLikeCount', ret.oschina.notice.newLikeCount);// 新赞的个数
			    		
			    		var notice_count = parseInt(ret.oschina.notice.atmeCount) + parseInt(ret.oschina.notice.msgCount) + parseInt(ret.oschina.notice.reviewCount) + parseInt(ret.oschina.notice.newFansCount) + parseInt(ret.oschina.notice.newLikeCount);
						$api.setStorage('notice_count', notice_count);// 通知总数
			    		setAllNoticeIco( notice_count );
				    }else if(typeof(ret.oschina.result.errorCode) != 'undefined' && parseInt(ret.oschina.result.errorCode) == 0){
				    	clearUserInfo( false );		// 清除本地信息并不刷新页面

				    }else{
						api.toast({
						    msg: '解析信息失败',
						    duration:2000,
						    location: 'middle'
						});

				    }
				});
		    }else {
				api.toast({
				    msg: '获取信息失败',
				    duration:2000,
				    location: 'middle'
				});
		       log('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode);
		    }
		});
    }
    
    // 设置通知数图标
    function setNoticeIco( notice_count ){
		if( parseInt(notice_count)>0 ){
    		$api.text($api.byId('notice_count'), notice_count);
			$('#notice_count').show();
		}else{
    		$api.text($api.byId('notice_count'), 0);		
			$('#notice_count').hide();
		}
    }
    
    function logout(){
		api.actionSheet({
			title: '退出当前帐号？',
		    cancelTitle: '取消',
		    destructiveTitle: '退出',
		},function(ret,err){
		    if(ret.buttonIndex == 1){
		    	clearUserCookie();
		    }
		});
    }
    
    // 清除Cookie
    function clearUserCookie(){
		api.showProgress({
		    style: 'default',
		    animationType: 'fade',
		    title: '注销中...',
		    text: '',
		    modal: true
		});
    
    	// 获取用户session
		api.ajax({
		    url: 'http://www.oschina.net/home/aboutosc',	// 这个页面内容较少，速度会快些
		    method: 'get',
		    timeout: 30,
		    dataType: 'text',
		    returnAll:false,
		},function(ret,err){
			if( ret ){
				var mSession = ret.match(/session=(.+)&goto_page/);
				if(null == mSession){
					clearUserInfo( true );	// 清除本地信息并刷新页面
			    	setAllNoticeIco( 0 );
				}else{
					var session = mSession[1];	// session 值

					api.ajax({
					    url: OpenAPI.logout + '?session=' + session,	// 注销
					    method: 'get',
					    timeout: 30,
					    dataType: 'text',
					    returnAll: true,
					},function(ret,err){
						if( typeof(ret.statusCode) != 'undefined' && parseInt(ret.statusCode)==200 ){
							clearUserInfo( true );	// 清除本地信息并刷新页面
					    	setAllNoticeIco( 0 );
						}else if( typeof(err.statusCode) != 'undefined' && parseInt(err.statusCode)==403 ){
							api.toast({
							    msg: '注销失败：403',
							    duration:2000,
							    location: 'middle'
							});
						}else{
							api.toast({
							    msg: '注销失败',
							    duration:2000,
							    location: 'middle'
							});
						}

					});
					
				}
			}else{
		        log('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode);
		    	showAjaxErrMsg(err.code);
			}
			api.hideProgress();
		});
    }
    
    
    // 打开消息中心
    function openNotice(){
		if( isLogin()==false ){
			api.toast({
			    msg: '请先登录',
			    duration:2000,
			    location: 'middle'
			});
			return false;
		}
    
		api.openWin({
		    name: 'notice_win',
		    url: 'notice_win.html',
		});
    }
    
    // 打开聊天室
    function openChatRoom(){
		if( isLogin()==false ){
			api.toast({
			    msg: '请先登录',
			    duration:2000,
			    location: 'middle'
			});
			return false;
		}
    
		api.openWin({
		    name: 'chat_room_win',
		    url: 'widget://html/chat_room_win.html',
		});
    }
</script>
</html>